package com.wu.sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class SudoKu extends Activity {

	private static final String TAG = "sudoku";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		int c = getResources().getColor(R.color.background);
		View btnContinue = this.findViewById(R.id.continue_button);
		btnContinue.setOnClickListener(new ContinueOnClickListener(this));
		View btnNewGame = this.findViewById(R.id.new_game_button);
		btnNewGame.setOnClickListener(new NewGameOnClickListener(this));
		View btnAbout = this.findViewById(R.id.about_button);
		btnAbout.setOnClickListener(new AboutOnClickListener(this));
		View btnExit = this.findViewById(R.id.exit_button);
		btnExit.setOnClickListener(new ExitOnClickListener(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		// TODO switch options action
		Log.v("fransk", "test");
		switch (item.getItemId()) {
		case R.id.settings:
			startActivity(new Intent(this, Prefs.class));
			return true;

		default:
			break;
		}

		return false;
	}

	protected void startGame(int i) {
		Log.d("sudoku", "clicked on " + i);
		Intent intent = new Intent(this, Game.class);
		intent.putExtra(Game.KEY_DIFFICULTY, i);
		this.startActivity(intent);
	}

}

class AboutOnClickListener implements OnClickListener {

	private Activity thisActivity = null;

	public AboutOnClickListener(Activity sudoKuActivity) {
		thisActivity = sudoKuActivity;
	}

	@Override
	public void onClick(View view) {
		Intent i = new Intent(thisActivity, About.class);
		thisActivity.startActivity(i);
	}

}

class ContinueOnClickListener implements OnClickListener {

	private Activity thisActivity = null;

	public ContinueOnClickListener(Activity sudoKuActivity) {
		thisActivity = sudoKuActivity;
	}

	@Override
	public void onClick(View view) {
		((SudoKu) thisActivity).startGame(Game.DIFFICULTY_CONTINUE);

		// Intent i = new Intent(thisActivity, Continue.class);
		// thisActivity.startActivity(i);
	}
}

class NewGameOnClickListener implements OnClickListener {

	private Activity thisActivity = null;

	public NewGameOnClickListener(Activity sudoKuActivity) {
		thisActivity = sudoKuActivity;
	}

	@Override
	public void onClick(View view) {
		openNewGameDialog();
		// Intent i = new Intent(thisActivity, NewGame.class);
		// thisActivity.startActivity(i);
	}

	private void openNewGameDialog() {
		new AlertDialog.Builder(thisActivity).setTitle(R.string.new_game_title)
				.setItems(R.array.difficulty,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface dialogInterface, int i) {
								((SudoKu) thisActivity).startGame(i);
							}
						}).show();

	}

}

class ExitOnClickListener implements OnClickListener {

	private Activity thisActivity = null;

	public ExitOnClickListener(Activity sudoKuActivity) {
		thisActivity = sudoKuActivity;
	}

	@Override
	public void onClick(View view) {
		thisActivity.finish();
	}

}